//import { GameManager } from './gameManager';
import { _decorator, Component, Node, Vec3, SkeletalAnimationState, AnimationClip, SkeletalAnimationComponent } from 'cc';
import { GameApp } from '../../GameApp';

//import { Constant } from '../framework/constant';
//import { AudioManager } from '../framework/audioManager';
const { ccclass, property } = _decorator;
//玩家角色模型脚本
@ccclass('PlayerModel')
export class PlayerModel extends Component {
    @property(Node)
    public ndSocketLoose: Node = null!//弓箭发射的节点

    @property(Node)
    public ndSocketHand: Node = null!//手节点

    @property(Node)
    public ndArrow: Node = null!;//攻击时候展示的弓箭

    @property(SkeletalAnimationComponent)
    public aniComPlayer: SkeletalAnimationComponent = null!;//动画播放组件

    public looseEulerAngles: Vec3 = new Vec3();//射箭时的角度
    public isAniPlaying: boolean = false;//当前动画是否正在播放



    private _aniType: string = "";//动画类型
    private _aniState: SkeletalAnimationState = null!;//动画播放状态
    private _stepIndex: number = 0;//脚步

    start () {
        // [3]
    }

    public init () {
        this.hideArrow();
    }

    /**
     * attack帧事件:箭射出去的时候触发
     * @returns 
     */
    public onFrameAttackLoose () {
 
        
        this.looseEulerAngles = this.node.parent?.eulerAngles as Vec3;
        this.ndArrow.active = false;

    }

    /**
     * run帧事件：脚落地的时候播放音效
     *
     * @memberof PlayerModel
     */
    public onFrameRun () {
    }

    /**
     * attack帧事件: 拉弓的时候触发
     */
    public onFrameAttackDraw () {
        this.ndArrow.active = true;
    }
    
    /**
     * 隐藏模型手中默认的箭
     */
    public hideArrow () {
        this.ndArrow.active = false;
    }
     //射箭
    onFrameAttack () {
        
 
      GameApp.Instance.playerTs.attickCallback()
        
      
    }
    public stopAni(){
        this._aniState=null
        this._aniType = "";
        this.aniComPlayer.stop()
    }
        /**
     * 播放玩家动画
     *
     * @param {string} aniType 动画类型
     * @param {boolean} [isLoop=false] 是否循环
     * @param {Function} [callback] 回调函数
     * @param {number} [callback] 调用播放动画的位置，方便用于测试
     * @returns
     * @memberof Player
     */
    public playAni (aniType: string, isLoop: boolean = false,callback?: Function,speed?:number,  pos?: number) {
        if(GameApp.Instance.playerTs.isStopAllAni){

            return
        }
        // console.log("playerAniType", aniType, "curAniType", this.aniType, "pos", pos);
        this._aniState = this.aniComPlayer?.getState(aniType) as SkeletalAnimationState;

        if (this._aniState && this._aniState.isPlaying) {
            return;
        }

        this._aniType = aniType;

        // if (this._aniType !== Constant.PLAYER_ANI_TYPE.ATTACK) {
        //     this.hideArrow();
        // }
      
        this.aniComPlayer?.play(aniType);
        
        this.isAniPlaying = true;

        if (this._aniState) {
            if (isLoop) {
                this._aniState.wrapMode = AnimationClip.WrapMode.Loop;    
            } else {
                this._aniState.wrapMode = AnimationClip.WrapMode.Normal;    
            }

            switch (aniType) {
                case "attack":
                 //   console.log("我是技能攻击了");
                    
                   if(speed){
                    this._aniState.speed = speed
                   }else{
                    this._aniState.speed = 1
                   }
                  

                   // GameApp.Instance.scriptPlayer.hideRunSmoke();
                    break;
                case "run":
                    
                //   GameApp.Instance.playerTs.playRunSmoke(); 
                    break;
                case "idle":
                //  GameApp.Instance.playerTs.hideRunSmoke();
                    break;
                default:
                    break;
            }
        }

        if (!isLoop) {
            this.aniComPlayer.once(SkeletalAnimationComponent.EventType.FINISHED, ()=>{
                this.isAniPlaying = false;
                callback && callback();
            })
        }
    }

    // update (deltaTime: number) {
    //     // [4]
    // }
}